﻿<%@ Page Title="城市核心码" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="../assets/css/shelf/base.css" rel="stylesheet">
    <link href="../assets/css/plugins/webuploader/webuploader.css" rel="stylesheet" />
    <link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />

    <style type="text/css">
        .system-box .right-side {
            width: 100%;
        }

        .condition, .condition-title {
            border-bottom: 1px solid #dfdfdd;
        }

            .condition .form-control {
                padding: 2px 6px;
            }

        .condition-title {
            padding: 4px 12px;
            font-size: 12px;
            font-weight: bold;
            background: #fff;
        }

        .replenishDay, .billandPickDay {
            width: 30px;
        }

        .setInput {
            width: 30px;
        }

        #paramSet, .system-box .right-side .ibox-content {
            /*overflow-x: auto!important;*/
            padding: 0;
        }

        #paramSet {
            padding: 5px;
        }

        #setTable, .loadTable {
            border-left: 1px solid #dfdfdd;
            border-bottom: 1px solid #dfdfdd;
        }

            #setTable th,
            #setTable td,
            .loadTable th,
            .loadTable td {
                border-top: 1px solid #dfdfdd;
                border-right: 1px solid #dfdfdd;
                text-align: center;
            }

            .loadTable th,
            .loadTable td {
                min-width: 30px;
            }

            .loadTable tr.check td {
                background: #dff0d8;
                border: 1px solid #7acf89;
            }

            #setTable td input {
                border: 0;
                text-align: center;
            }

        .hasNum {
            font-weight: bold;
            color: #f00;
        }

            .hasNum.notCore {
                color: #0000FF;
            }

        #tb-head-container {
            padding-top: 5px;
        }

        #tb-body-container {
            /*overflow: auto;*/
        }

        .tr-core-code {
            height: 30px
        }

        .input-w65 {
            width: 65px !important;
        }

        .conditions {
            clear: both;
            margin: 5px 0;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container"></div>
            <div class="condition">
                <label>类别</label>
                <select class="form-control input-w150" id="search-kindID">
                </select>
                <label>版型</label>
                <select class="form-control input-w110" id="search-ClothesVersion">
                    <option value="">空</option>
                </select>
                <label>性别</label>
                <select class="form-control input-w65" id="search-sexName">
                </select>
                <label>关键字</label>
                <input type="text" class="form-control input-w170" id="search-keys" placeholder="城市名称或编号" />
                <label>商品区域</label>
                <select class="form-control input-w120" id="search-commodity">
                    <option value="">全部</option>
                </select>
                <label>省份</label>
                <select class="form-control input-w120" id="search-province">
                    <option value="">全部</option>
                </select>
                <label>城市</label>
                <select class="form-control input-w120" id="search-city">
                    <option value="">全部</option>
                </select>
            </div>
            <div class="condition-title">陈列量设置（注:相应尺码设置了陈列量系统默认为核心尺码）</div>
            <div id="paramSet" class="condition">
                <table id="setTable" cellpadding="0" cellspacing="0">
                    <thead></thead>
                    <tbody id="setList"></tbody>
                </table>
            </div>

            <div style="overflow: auto; background: #fff;">

                <div id="tb-head-container">
                    <div class="ibox-content">
                        <table class="loadTable">
                            <thead></thead>
                        </table>
                    </div>
                </div>

                <div id="tb-body-container">
                    <div class="ibox-content">
                        <table class="loadTable">
                            <tbody id="loadList"></tbody>
                        </table>
                    </div>
                </div>

            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/plugins/webuploader/webuploader.min.js"></script>
    <script src="../assets/js/framework.js"></script>
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>

    <script type="application/javascript">
        $('document').ready(function () {
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");

            //判断浏览器类型
            var sUserAgent = navigator.userAgent.toLowerCase();
            if (sUserAgent.indexOf('chrome') > -1) {
                $('#tb-head-container').css({ 'paddingRight': '6px' });
            } else {
                $('#tb-head-container').css({ 'paddingRight': '15px' });
            }

            $('#tb-body-container').height(
                $('.right-side').height() - $('.condition-title').outerHeight() -
                $('.condition').outerHeight() - $('#tb-head-container').outerHeight() - (sUserAgent.indexOf('chrome') > -1 ? 155 : 154));

            $(window).resize(function () {
                $('#tb-body-container').height(
                    $('.right-side').height() - $('.condition-title').outerHeight() -
                    $('.condition').outerHeight() - $('#tb-head-container').outerHeight() - (sUserAgent.indexOf('chrome') > -1 ? 123 : 125));
            })

            //省份列表
            var provinceList = [];
            //获取按钮
            commonUtil.loadBtnList(ModuleID, function () { });
            var kindId = $('#search-kindID').val();
            //选择性别 
            var sexName = $('#search-sexName').val();
            //选择版型
            var ClothesVersion = $("#search-ClothesVersion").val();
            //初始化下拉框
            //商品区域
            commonUtil.bindDwGlobalVariables({ dictValue: '商品区域' }, ['#search-commodity']);

            //省份&城市
            var loadAreaList = function () {
                Y.API2('OdsErpBasArea', 'GetOdsErpBasAreaList', {
                    Param: 1,
                    DistrictID: $('#search-commodity').val()
                }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        provinceList = [];
                        data.map(function (item) {
                            provinceList.push(item);
                            $('#search-province').append('<option value="' + item[0].ParentID + '">' + item[0].ParentName + '</option>');
                        })
                    }
                });
            }

            //选择商品区域
            $('#search-commodity').change(function () {
                $('#search-province').empty();
                $('#search-province').append('<option value="">全部</option>');
                $('#search-city').empty();
                $('#search-city').append('<option value="">全部</option>');
                if ($(this).val() == '')
                    return false;
                loadAreaList();
            })
            //如果是男.女装可以选择版型，因为是默认男装，如果后期修改，请在load方法添加默认加载时disabled
            $('#search-kindID').change(function () {
                var val = $('#search-kindID').find("option:selected").text();
                if (val == "男装" || val == "女装")
                    $("#search-ClothesVersion").removeAttr("disabled");
                else {
                    $("#search-ClothesVersion").attr("disabled", "disabled");
                    $("#search-ClothesVersion").val("");
                    $("#search-ClothesVersion").find("option[text='空']").attr("selected", true);
                }

            })


            //选择省份
            $('#search-province').change(function () {
                $('#search-city').empty();
                $('#search-city').append('<option value="">全部</option>');
                var $this = $(this);
                provinceList.map(function (item) {
                    if (item[0].ParentID == $this.val()) {
                        console.log(item[0].ParentID + ">" + $this.val())
                        item.map(function (city) {
                            $('#search-city').append('<option value="' + city.AreaID + '">' + city.AreaName + '</option>');
                        });
                        $('#search-city').val('');
                        return false;
                    }
                })
            });

            //批量添加任务
            $(".btns-container").on("click", '#importBtn', function () {
                $.ImportExcelTemplate({
                    filetype: 'DWSIZETYPE',
                    filename: '城市核心码',
                    conditions: getConditions(),
                    action: 'ImportTemplate',
                    state: 'hasInfo',
                    after: function () {
                        //回调方法
                        loadList();
                    }
                });
            });

            //加载列表
            var loadList = function (init) {
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                var keys = $('#search-keys').val().trim().replace(/'/g, '');
                Y.API2('SizeType', 'GetSizeType', {
                    Keys: keys,
                    ShopArea: $('#search-commodity').val(),
                    Province: $('#search-province').val(),
                    City: $('#search-city').val(),
                    KindID: $("#search-kindID").val(),
                    SexName: $("#search-sexName").val(),
                    ClothesVersion: $('#search-ClothesVersion').val(),
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        var loadBody = [];
                        data.SizeList.map(function (item, i) {
                            var tempChild = [];
                            tempChild.push({ tdText: '', c: 0, r: 0, tdHtml: '<span style="display:inline-block; width:46px;"><input type="checkbox" class="checkItem" /></span>', tdId: '', tdClass: 'notClick diy-td-w46' });
                            tempChild.push({ tdText: '', c: 0, r: 0, tdHtml: '<span style="display:inline-block; width:250px;">' + item[0].FullName.replace('.', ' - ') + '</span><input type="hidden" class="cityId" value="' + item[0].AreaID + '" />', tdId: '', tdClass: 'diy-td-w250' });
                            data.ErpSizeList.map(function (es) {
                                var count = 0;
                                item.map(function (it) {
                                    if (it.SizeName == es.SizeName) {
                                        tempChild.push({ tdText: '', c: 0, r: 0, tdHtml: '<span style="display:inline-block; width:46px;">' + (Y.checkNull(it.DisplayQty) || it.DisplayQty == 0 ? '' : it.DisplayQty) + '</span>', tdId: '', tdClass: (it.DisplayQty == 0 ? '' : 'hasNum ') + (it.SizeCoreFlag == 1 ? '' : 'notCore ') + es.SizeName + ' diy-td-w46' });
                                        return false;
                                    }
                                    count++;
                                })
                                if (count == item.length) {
                                    tempChild.push({ tdText: '', c: 0, r: 0, tdHtml: '<span style="display:inline-block; width:46px;"></span>', tdId: '', tdClass: es.SizeName + ' diy-td-w46' });
                                }
                            })

                            var tempTr = {
                                trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
                                children: tempChild
                            };
                            loadBody.push(tempTr);
                        })
                        tableControl.initTbody('.loadTable', (loadBody.length > 0 ? loadBody : null), (data.ErpSizeList.length + 1), null);

                        //判断是否为初始化
                        //					if(init){
                        $('#setTable thead').empty();
                        $('#setTable tbody').empty();
                        //处理设置部分
                        var setHeadList = ['尺码'];
                        var setBody = [];
                        var setChildren = [];
                        var setChildren2 = [];
                        var loadHeadList = [];
                        var isNum = 0;
                        setChildren.push({ tdText: '陈列量', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' });
                        setChildren2.push({ tdText: '是否核心码', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'tr-core-code' });
                        data.ErpSizeList.map(function (item) {
                            var tempSize = parseInt(item.SizeName);
                            if (!isNaN(tempSize) && tempSize.toString().length == item.SizeName.length) {
                                isNum++;
                            }
                            setHeadList.push(item.SizeName);
                            setChildren.push(
                                {
                                    tdText: '', c: 0, r: 0, tdHtml: '<input type="text" maxlength="2" class="setInput ' +
                                    item.SizeName + (item.SizeCoreFlag == 1 ? '' : ' notCore') + '" />', tdId: '', tdClass: ''
                                }
                            );
                            setChildren2.push(
                                {
                                    tdText: '', c: 0, r: 0, tdHtml: '<input type="checkbox" class="isCore ' +
                                    item.SizeName + (item.SizeCoreFlag == 1 ? '' : ' notCore') + '"' + (item.SizeCoreFlag == 1 ? 'checked="checked"' : '') + ' />', tdId: '', tdClass: 'tr-core-code'
                                }
                            );
                            loadHeadList.push(
                                { tdText: '', c: 0, r: 0, tdHtml: '<span style="display:inline-block; width:46px;">' + item.SizeName + '</span>', tdId: '', tdClass: 'diy-td-w46' }
                            )
                        })
                        var tempTr = { trClass: '', children: setChildren };
                        var tempTr2 = { trClass: '', children: setChildren2 };
                        setBody.push(tempTr);
                        setBody.push(tempTr2);
                        tableControl.initThead2('#setTable', setHeadList);
                        tableControl.initTbody('#setTable', (setBody.length > 0 ? setBody : null), data.ErpSizeList.length, null);
                        tableControl.initThead('.loadTable', [
                            {
                                trClass: '', children: [
                                    { tdText: '', c: 0, r: 2, tdHtml: '<span style="display:inline-block; width:46px;"><input type="checkbox" class="checkAll" /></span>', tdId: '', tdClass: 'diy-td-w46' },
                                    { tdText: '', c: 0, r: 2, tdHtml: '<span style="display:inline-block; width:250px;">城市</span>', tdId: '', tdClass: 'diy-td-w250' },
                                    { tdText: '字母码', c: (data.ErpSizeList.length - isNum), r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                    { tdText: '数字码', c: isNum, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                ]
                            },
                            { trClass: '', children: loadHeadList }
                        ])
                        //					}
                    }

                    //选择全部插件 2017-05-02 改
                    $("#setTable input[type='checkbox']").iCheck({ checkboxClass: "icheckbox_square-green" });
                    $(".loadTable").icheckAll({
                        checkObj: ".checkItem",//表格下面的选择控件对象 选择器
                        checkAllObj: ".checkAll",//全选 选择器
                        clickoutBoxCheck: false,//点击行 不选中
                        checkboxMargin: "4px 0 0 0 "//上右下左
                    });

                })

            }
            getKindList();

            //性别
            function getGenderList(callback) {
                $('#search-sexName').empty();
                //$('#search-sexName').append('<option value="">全部</option>');
                Y.API2('SizeType', 'GetGenderList', {}, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        var html = '';
                        data.map(function (item) {
                            if (item.SexName == "男")
                                html += '<option value=' + item.SexName + ' selected="selected" >' + item.SexName + '</option>';
                            else
                                html += '<option value=' + item.SexName + '>' + item.SexName + '</option>';
                        });
                        $('#search-sexName').append(html);
                        //第一次加载，默认 男。
                        sexName = $('#search-sexName').val();
                        //加载完性别再加载数据。
                        loadList(true);
                        if (typeof callback === "function") {
                            callback();
                        }
                    }
                });
                GetClothesVersionList(false);
            }
            //获取版型下拉框数据
            function GetClothesVersionList(callback) {
                $('#search-ClothesVersion').empty();
                $('#search-ClothesVersion').append('<option value="">空</option>');
                Y.API2('SizeType', 'GetClothesVersionList', {}, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        var html = '';
                        data.map(function (item) {
                            html += '<option value=' + item.ClothesVersionCode + '>' + item.ClothesVersionName + '</option>';
                        });
                        $('#search-ClothesVersion').append(html);
                        var val = $('#search-kindID').find("option:selected").text();
                        if (val == "男装" || val == "女装")
                            $("#search-ClothesVersion").removeAttr("disabled");
                        else {
                            $("#search-ClothesVersion").attr("disabled", "disabled");
                        }
                        if (typeof callback === "function") {
                            callback();
                        }
                    }
                });
            }

            //城市核心码获取类别
            function getKindList(callback) {
                $('#search-kindID').empty();
                //  $('#search-kindID').append('<option value="">全部</option>');
                Y.API2('SizeType', 'GetKindList', {}, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        var html = '';
                        data.map(function (item) {
                            //if (item.KindName == "男装")
                            //    html += '<option value=' + item.KindID + ' selected="selected" >' + item.KindName + '</option>';
                            //else
                            html += '<option value=' + item.KindID + '>' + item.KindName + '</option>';
                        });
                        $('#search-kindID').append(html);

                        kindId = $('#search-kindID').val();
                        if (typeof callback === "function") {
                            callback();
                        }

                        //先加载性别选择再load数据，load方法放到数据里面 
                        getGenderList();
                    }
                });
            }

            ////全选 2017-05-02注释
            //$('.loadTable').on('click','.checkAll',function(){
            //	if(this.checked)
            //		$('#loadList .checkItem').each(function(){this.checked=true;});
            //	else
            //		$('#loadList .checkItem').each(function(){this.checked=false;});
            //})

            ////选中城市 2017-05-02注释
            //$('.loadTable').on('click','.checkItem',function(){
            //	if($('#loadList .checkItem').size() == $('#loadList .checkItem:checked').size())
            //		$('.loadTable .checkAll').prop("checked", true);
            //	else
            //		$('.loadTable .checkAll').prop("checked", false);
            //});

            //选中行
            $('.loadTable').on('click', 'td', function () {
                if (!$(this).hasClass('notClick')) {
                    $('.loadTable tr').removeClass('check');
                    var tr = $(this).parents('tr');
                    tr.addClass('check');
                    $('.setInput').removeClass('hasNum');
                    $('.setInput').removeClass('notCore');
                    tr.children().each(function (index) {
                        if (index > 1) {
                            if (!isNaN(parseInt($(this).text())) && parseInt($(this).text()) > 0)
                                $('.setInput').eq(index - 2).addClass('hasNum');
                            if ($(this).hasClass('notCore'))
                                $('.setInput').eq(index - 2).addClass('notCore');
                            $('#setTable .isCore').eq(index - 2).iCheck((!$(this).hasClass('notCore') && $(this).text().trim() != '') ? "check" : "uncheck");
                            //$('#setTable .isCore').eq(index - 2).prop("checked", (!$(this).hasClass('notCore') && $(this).text().trim() != ''));
                            $('.setInput').eq(index - 2).val($(this).text());
                        }
                    })
                }
            });

            //查询按钮
            $('.btns-container').on('click', '#btn-search', function () {
                $('.loadTable .checkAll').prop("checked", false);
                kindId = $('#search-kindID').val();
                sexName = $('#search-sexName').val();
                ClothesVersion = $('#search-ClothesVersion').val();
                loadList();
            });
            //刷新按钮 
            $('.btns-container').on('click', '#refreshBtn', function () {
                window.location.reload();
            });

            //批量修改
            $('.btns-container').on('click', '#batchSet', function () {
                if ($('.loadTable .checkItem:checked').size() == 0) {
                    swal('至少选择一个城市');
                    return false;
                }
                var countNaN = 0;
                $('.setInput').each(function () {
                    if (isNaN(parseInt($(this).val())))
                        countNaN++;
                })
                if ($('.setInput').size() == countNaN) {
                    return false;
                }


                if ($("#search-ClothesVersion").val() != "" && $("#search-kindID").val() != "000001" && $("#search-kindID").val() != "000002") {
                    swal('版型不为空时,类别只能是男装或女装！');
                    return false;
                }

                var list = []
                $('.loadTable .checkItem:checked').each(function () {
                    var children = [];
                    $('.setInput').each(function (index) {
                        var tempValue = parseInt($(this).val());
                        tempValue = (tempValue == 0 || isNaN(tempValue) ? '' : tempValue);
                        if (tempValue != '') {
                            children.push({
                                SizeName: $('#setTable th').eq(index + 1).text(),
                                DisplayQty: tempValue,
                                KindID: $("#search-kindID").val(),
                                SizeCoreFlag: $('.isCore').eq(index).is(':checked') ? 1 : 0,
                                sexName: $("#search-sexName").val(),
                                ClothesVersionCode: $("#search-ClothesVersion").val()
                            });
                        }
                    });
                    var temp = {
                        CityID: $(this).parents('tr').find('.cityId').val(),
                        KindID: $("#search-kindID").val(),
                        Children: children,
                        sexName: $("#search-sexName").val(),
                        ClothesVersionCode: $("#search-ClothesVersion").val()
                    }
                    list.push(temp);
                });

                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2('SizeType', 'BatchSizeType', {
                    list: list
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal('修改成功', '', 'success');
                        loadList();
                    }
                })
            })
        });

        //获取筛选条件
        var getConditions = function () {
            var conditionJSON = [];
            //关键字
            if ($('#search-keys').val().length > 0) {
                conditionJSON.push({ column: "a.FullName", value: $('#search-keys').val(), operator: "like", connector: "and" });
            }
            //商品区域
            if ($('#search-commodity').val().length > 0) {
                conditionJSON.push({ column: "c.ParentAreaID", value: $('#search-commodity').val(), operator: "=", connector: "and" });
            }
            //省份
            if ($('#search-province').val().length > 0) {
                conditionJSON.push({ column: "a.ParentID", value: $('#search-province').val(), operator: "=", connector: "and" });
            }
            //城市
            if ($('#search-city').val().length > 0) {
                conditionJSON.push({ column: "a.AreaID", value: $('#search-city').val(), operator: "=", connector: "and" });
            }
            //类别（核心码页面）
            if ($('#search-kindID').val().length > 0) {
                conditionJSON.push({ column: "KindID", value: $('#search-kindID').val(), operator: "=", connector: "and" });
            }
            //性别
            //if ($('#search-sexName').val().length > 0) {
            //    conditionJSON.push({ column: "SexName", value: $('#search-sexName').val(), operator: "like", connector: "and" });
            //}

            conditionJSON = JSON.stringify(conditionJSON);
            var conditions = encodeURIComponent(conditionJSON);
            return conditions;
        };

    </script>
</asp:Content>

